<?php

namespace common\service;

use common\exception\LogicException;
use common\model\EntityModel;
use common\model\TaskModel;
use support\ErrorCode;
use support\SingletonTrait;
use think\Exception;

class TaskEnableService
{

    use SingletonTrait;

    /**
     * 恢复任务
     * @param $taskList
     * @return array
     * @throws \think\Exception
     */
    public function recoveryTask($taskList)
    {
        $updateData = [];
        if (empty($taskList)) {
            throw new LogicException('task data is null', ErrorCode::GET_TASK_DATA_FAIL);
        }
        $progressStatus = StatusService::getInstance()->getTaskDefaultStartStatus();

        foreach ($taskList as $val) {
            $jsonOld = json_decode($val['json'], true);
            if (empty($jsonOld['task_deprecate_data'])) {
                $json = $jsonOld;
                $json['task_status'] = $progressStatus['id'];
            } else {
                $json = $jsonOld['task_deprecate_data'];
            }
            $json['task_deprecate_data'] = '';
            $updateData[] = [
                'id' => $val['id'],
                'json' => $json,
            ];
        }

        // 批量更新数据
        if (!empty($updateData)) {
            $updateData = CommonService::instance('task')->commonBatchUpdate($updateData);
        }
        return $updateData;
    }

}
